{
 "cells": [
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.0608,  0.1294,  0.0163, -0.0565,  0.0391,  0.0978, -0.1404, -0.0045,\n         -0.0766, -0.0251],\n        [ 0.0816,  0.0837, -0.0572, -0.0486,  0.0490,  0.0970, -0.1274, -0.0255,\n          0.0297, -0.0071]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "\n",
    "net = nn.Sequential(nn.Linear(20, 256), nn.ReLU(), nn.Linear(256, 10))\n",
    "\n",
    "X = torch.rand(2, 20)\n",
    "net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:20:54.306013Z",
     "start_time": "2024-03-30T12:20:50.320286Z"
    }
   },
   "id": "86bca428b8f45875",
   "execution_count": 1
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.1.1. 自定义块"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "45dad6d9e2672fd3"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class MLP(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.hidden = nn.Linear(20, 256)\n",
    "        self.out = nn.Linear(256, 10)\n",
    "\n",
    "    def forward(self, X):\n",
    "        return self.out(F.relu(self.hidden(X)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:26:58.063470Z",
     "start_time": "2024-03-30T12:26:58.050362Z"
    }
   },
   "id": "bb91e5724de91657",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[-0.1010,  0.1120, -0.0873,  0.1026, -0.1364, -0.2625,  0.0908, -0.1066,\n          0.1512, -0.0033],\n        [-0.2222,  0.0871,  0.0168,  0.1870, -0.1546, -0.1957,  0.0482,  0.0087,\n          0.1808, -0.1249]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = MLP()\n",
    "net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:27:57.113954Z",
     "start_time": "2024-03-30T12:27:57.100712Z"
    }
   },
   "id": "1b2dec2125f2b8eb",
   "execution_count": 4
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.1.2. 顺序块"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "677c449ff4599910"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class MySequential(nn.Module):\n",
    "    def __init__(self, *args):\n",
    "        super().__init__()\n",
    "        for idx, module in enumerate(args):\n",
    "            self._modules[str(idx)] = module\n",
    "\n",
    "    def forward(self, X):\n",
    "        for block in self._modules.values():\n",
    "            X = block(X)\n",
    "        return X"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:32:47.469606Z",
     "start_time": "2024-03-30T12:32:47.451036Z"
    }
   },
   "id": "616f16d658526730",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 0.0310, -0.0345,  0.0732,  0.0454, -0.0041, -0.1000, -0.0795, -0.1824,\n          0.1749, -0.1048],\n        [-0.0961, -0.0479,  0.0349,  0.0635, -0.0889, -0.2191,  0.0370, -0.0850,\n         -0.1415, -0.0457]], grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = MySequential(nn.Linear(20, 256), nn.ReLU(), nn.Linear(256, 10))\n",
    "net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:32:47.750215Z",
     "start_time": "2024-03-30T12:32:47.737074Z"
    }
   },
   "id": "82e8f1b04423dee0",
   "execution_count": 11
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5.1.3. 在前向传播函数中执行代码"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e37bcec578b4d45b"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[0.4008, 0.5846, 0.2553],\n         [0.9711, 0.1676, 0.5560]]),\n tensor([[ 1.1050,  0.2773, -0.9953, -0.5102],\n         [-0.5506,  0.5595, -1.4142, -1.9503],\n         [-2.0794,  2.1168,  0.2402,  0.9875]]))"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.rand((2,3), requires_grad=False), torch.randn((3,4), requires_grad=False)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:35:20.064944Z",
     "start_time": "2024-03-30T12:35:20.018746Z"
    }
   },
   "id": "7453238a643b820",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class FixedHiddenMPL(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.rand_weight = torch.rand((20,20), requires_grad=False)\n",
    "        self.linear = nn.Linear(20, 20) \n",
    "        \n",
    "    def forward(self, X):\n",
    "        X = self.linear(X)\n",
    "        X = F.relu(torch.mm(X, self.rand_weight) + 1)\n",
    "        X = self.linear(X)\n",
    "        while X.abs().sum() > 1:\n",
    "            X /= 2\n",
    "        return X.sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:37:28.887759Z",
     "start_time": "2024-03-30T12:37:28.882897Z"
    }
   },
   "id": "839374beeb50ddda",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(-0.1741, grad_fn=<SumBackward0>)"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = FixedHiddenMPL()\n",
    "net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:39:48.724758Z",
     "start_time": "2024-03-30T12:39:48.707863Z"
    }
   },
   "id": "ae143fa248a51b54",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class NestedMLP(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.net = nn.Sequential(nn.Linear(20,64), nn.ReLU(), nn.Linear(64, 32), nn.ReLU())\n",
    "        self.linear = nn.Linear(32, 16)\n",
    "        \n",
    "    def forward(self, X):\n",
    "        return self.linear(self.net(X))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:41:22.619313Z",
     "start_time": "2024-03-30T12:41:22.607206Z"
    }
   },
   "id": "cbcafcb0a4a3cefb",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[-0.1552,  0.0092,  0.1164,  0.0190,  0.1854, -0.0137,  0.0797,  0.0664,\n          0.0267,  0.2124,  0.0293, -0.2806, -0.1062,  0.1745, -0.0168, -0.0468],\n        [-0.1289, -0.0447,  0.1140, -0.0583,  0.1957,  0.0195,  0.0424,  0.0833,\n          0.0379,  0.1675,  0.0415, -0.2115, -0.1584,  0.1310,  0.0134, -0.0557]],\n       grad_fn=<AddmmBackward0>)"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = NestedMLP()\n",
    "net(X)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-30T12:41:30.883269Z",
     "start_time": "2024-03-30T12:41:30.864827Z"
    }
   },
   "id": "864d94c37fb3d258",
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "88c25184c93b9924"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
